﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate;
using NHibernate.Criterion;

using OpenGado.DATA.Entities;

namespace OpenGado.DATA
{
   public class RequestItemDataControl
   {

       private static RequestItemDataControl requestItemDataControl;
       private static object lockRequestDataContorol;

       private ISession session;

       public ISession Session
       {
           get
           {
               if (session == null)
               {
                   session = FactoryHandler.CreateFactory().OpenSession();
                   
               }
               return session;
           }
      }



       public static RequestItemDataControl Instance
       {
           get
           {
                lock(lockRequestDataContorol)
                    if(lockRequestDataContorol ==null)
                    {
                        requestItemDataControl = new RequestItemDataControl();
                    }
               return requestItemDataControl;
           }
       }
       /// <summary>
       /// Saves request item
       /// </summary>
       /// <param name="requestItem"></param>
       public void Save(RequestItem requestItem)
       {

           this.Session.SaveOrUpdate(requestItem);
       }

       /// <summary>
       /// Saves request item header
       /// </summary>
       /// <param name="requestHeader"></param>
      public void  Save(RequestHeader requestHeader)
      {
          this.Session.SaveOrUpdate(requestHeader);
      }

       /// <summary>
       /// Saves teh whole product request line.
       /// </summary>
       /// <param name="product"></param>
       /// <param name="requestHeader"></param>
       /// <param name="quantity"></param>
       public  void Save(Product product, RequestHeader requestHeader, decimal quantity)
       {

           var requestItem = new RequestItem(requestHeader, product, quantity);
           this.Session.SaveOrUpdate(requestItem);
       }

       /// <summary>
       /// Deletes the request item
       /// </summary>
       /// <param name="requestItem"></param>
       public void Delete (RequestItem requestItem)
       {
           this.Session.Delete(requestItem);
       }


       /// <summary>
       /// Gets a list of all request items.
       /// </summary>
       /// <returns></returns>
       public  IList<RequestItem> GetListofAllRequests()
       {

           ICriteria criteria = Session.CreateCriteria<RequestItem>();
           IList<RequestItem> requestItems = criteria.List<RequestItem>();

           return requestItems;
       }


       /// <summary>
       /// Gets a list of all request items.
       /// </summary>
       /// <returns></returns>
       public IList<RequestItem> GetListofRequestsPerHeader(RequestHeader requestHeader)
       {

           ICriteria criteria = Session.CreateCriteria<RequestItem>();
           criteria.Add(Expression.Eq("RequestHeaderId", requestHeader.RequestHeaderId));

           IList<RequestItem> requestItems = criteria.List<RequestItem>();

           return requestItems;
       }
       public IList<RequestItem> GetListofRequestsPerHeader(int requestHeaderId)
       {

           ICriteria criteria = Session.CreateCriteria<RequestItem>();
           criteria.Add(Restrictions.Eq("RequestHeaderId", requestHeaderId));
           IList<RequestItem> requestItems = criteria.List<RequestItem>();

           return requestItems;
       }

       /// <summary>
       /// Returns a single Request header object
       /// </summary>
       /// <param name="requestHeaderId"></param>
       /// <returns></returns>

       public RequestHeader GetHeaderByid(int requestHeaderId)
       {
         
       
          return this.Session.Get<RequestHeader>(requestHeaderId);

       }
       /// <summary>
       /// Returns a single request item object
       /// </summary>
       /// <param name="requestItem"></param>
       /// <returns></returns>
       public RequestItem GetRequestItemById (int requestItem)
       {
           return this.Session.Get<RequestItem>(requestItem);
       }

   }
}
